<?php
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

class Diandian extends BaseOauth{

	protected $GetRequestCodeURL = 'https://api.diandian.com/oauth/authorize';
	protected $GetAccessTokenURL = 'https://api.diandian.com/oauth/token';
	protected $ApiBase = 'https://api.diandian.com/v1/';

	public function call($api, $param = '', $method = 'GET', $multi = false){
		$params = array(
			'access_token' => $this->Token['access_token'],
		);
		$data = $this->http($this->url($api, '.json'), $this->param($params, $param), $method);
		return json_decode($data, true);
	}

	protected function parseToken($result, $extend){
		$data = json_decode($result, true);
		if($data['access_token'] && $data['expires_in'] && $data['token_type'] && $data['uid']){
			$data['openid'] = $data['uid'];
			unset($data['uid']);
			return $data;
		} else
			throw new Exception("获取点点网ACCESS_TOKEN出错：{$data['error']}");
	}

	public function openid(){
		$data = $this->Token;
		if(isset($data['openid']))
			return $data['openid'];
		else
			throw new Exception('没有获取到点点网用户ID！');
	}

	public function getuserinfo($token){
		$data  = $this->call('user/info');

		if(!empty($data['meta']['status']) && $data['meta']['status'] == 200){
			$userInfo['type'] = 'DIANDIAN';
			$userInfo['name'] = $data['response']['name'];
			$userInfo['nick'] = $data['response']['name'];
			$userInfo['head'] = "https://api.diandian.com/v1/blog/{$data['response']['blogs'][0]['blogUuid']}/avatar/144";
			return $userInfo;
		} else {
			throw_exception("获取点点用户信息失败：{$data}");
		}
	}
}
